package com.wolves.career.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolves.career.entity.IndustryInformationInformation;
import com.wolves.career.entity.excel.InfoExcel;
import com.wolves.career.listener.InfoExcelListener;
import com.wolves.career.mapper.IndustryInformationInformationMapper;
import com.wolves.career.service.IndustryInformationClassificationService;
import com.wolves.career.service.IndustryInformationInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-20
 */
@Service
public class IndustryInformationInformationServiceImpl extends ServiceImpl<IndustryInformationInformationMapper, IndustryInformationInformation> implements IndustryInformationInformationService {
    @Autowired
    IndustryInformationInformationService informationService;
    @Autowired
    IndustryInformationClassificationService classificationService;

    @Override
    public QueryWrapper<IndustryInformationInformation> getInfo(IndustryInformationInformation info) {
        if (info == null) {
            return null;
        }
        String title = info.getTitle();
        String author = info.getAuthor();
        String content = info.getContent();
        Date createTime = info.getCreateTime();
        Date updateTime = info.getUpdateTime();
        String releaseStaffId = info.getReleaseStaffId();
        Integer views = info.getViews();
        String classificationId = info.getClassificationId();
        QueryWrapper<IndustryInformationInformation> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            //模糊查询标题
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(author)) {
            //模糊查询作者
            queryWrapper.like("author", author);
        }
        if (!StringUtils.isEmpty(content)) {
            //模糊查询内容
            queryWrapper.like("content", content);
        }
        if (!StringUtils.isEmpty(createTime)) {
            //查询比创建时间更早的
            queryWrapper.gt("create_time", createTime);
        }
        if (!StringUtils.isEmpty(updateTime)) {
            queryWrapper.lt("update_time", updateTime);
        }
        if (!StringUtils.isEmpty(releaseStaffId)) {
            queryWrapper.eq("release_staff_id", releaseStaffId);
        }
        if (!StringUtils.isEmpty(views)) {
            //查询比该点击量更高的
            queryWrapper.gt("views", views);
        }
        if (!StringUtils.isEmpty(classificationId)) {
            queryWrapper.eq("classification_id", classificationId);
        }
        return queryWrapper;
    }

    @Override
    public void importClassificationData(MultipartFile file,
                                         IndustryInformationClassificationService classificationService,
                                         IndustryInformationInformationService informationService) {
        try {
            EasyExcel.read(file.getInputStream(), InfoExcel.class, new InfoExcelListener(classificationService, informationService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
